<?php

/**
 * This is the model class for table "{{introducing_company}}".
 *
 * The followings are the available columns in table '{{introducing_company}}':
 * @property integer $id
 * @property string $code
 * @property string $name
 * @property string $english_name
 * @property string $short_name
 * @property string $address
 * @property string $phone_number
 * @property string $representative
 * @property string $title
 * @property string $job_approver
 * @property string $create_date
 * @property string $created_by
 * @property string $update_date
 * @property string $updated_by
 */
class IntroducingCompany extends CActiveRecord
{
	public $keyword;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return IntroducingCompany the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{introducing_company}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array ('code, name','required', 'message' => t ( 'MSG_REQUIRED', 'messages' )),
			array('code, short_name, representative, title, job_approver, created_by, updated_by', 'length', 'max'=>45, 'message' => t ( 'MSG_WRONG_NUMBER', 'messages' )),
			array('name, english_name, address', 'length', 'max'=>100, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('phone_number', 'length', 'max'=>200, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('keyword, id, code, name, english_name, short_name, address, phone_number, representative, title, job_approver, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'advanceProfile' => array (
					self::HAS_MANY,
					'AdvanceProfile',
					'introducing_company_id'
			)
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID','introducingCompany'),
			'code' => t('LBL_CODE','introducingCompany'),
			'name' => t('LBL_NAME','introducingCompany'),
			'english_name' => t('LBL_ENGLISH_NAME','introducingCompany'),
			'short_name' => t('LBL_SHORT_NAME','introducingCompany'),
			'address' => t('LBL_ADDRESS','introducingCompany'),
			'phone_number' => t('LBL_PHONE_NUMBER','introducingCompany'),
			'representative' => t('LBL_REPRESENTATIVE','introducingCompany'),
			'title' => t('LBL_TITLE','introducingCompany'),
			'job_approver' => t('LBL_JOB_APPROVER','introducingCompany'),
			'create_date' => t('LBL_CREATE_DATE','introducingCompany'),
			'created_by' => t('LBL_CREATED_BY','introducingCompany'),
			'update_date' => t('LBL_UPDATE_DATE','introducingCompany'),
			'updated_by' => t('LBL_UPDATED_BY','introducingCompany'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->addSearchCondition('name', $this->keyword, true, 'OR');
		$criteria->addSearchCondition('code', $this->keyword, true, 'OR');
		$criteria->compare('id',$this->id);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('english_name',$this->english_name,true);
		$criteria->compare('short_name',$this->short_name,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('phone_number',$this->phone_number,true);
		$criteria->compare('representative',$this->representative,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('job_approver',$this->job_approver,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function beforeSave() {
		if (parent::beforeSave()) {
	
			$current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
	
			//Get user is logging
			$logedInUser = User::model()->findByPk(Yii::app()->user->id);
	
			if ($this->isNewRecord) {
				$this->create_date = $current_date;
				$this->created_by = $logedInUser->username;
			}
	
			$this->update_date = $current_date;
	
			if (isset($logedInUser)) {
				$this->updated_by = $logedInUser->username;
			}
	
			$this->setIsNewRecord(false);
			return true;
		} else {
			return false;
		}
	}
	
	public function getChtmlDropdownData(){
		$listData = $this->findAll();
		return CHtml::listData($listData, 'id', 'name');
	}
}